package com.shenmazong.serverwebpoem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.serverwebpoem.mapper.ITbCollectionMapper;
import com.shenmazong.serverwebpoem.pojo.TbCollection;
import com.shenmazong.serverwebpoem.service.TbCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/24 8:32 AM
 * http://www.shenmazong.com
 */

@Slf4j
@Service
public class TbCollectionServiceImpl implements TbCollectionService {


    @Autowired
    ITbCollectionMapper iTbCollectionMapper;

    @Override
    public void add(TbCollection collection) {
        iTbCollectionMapper.insert(collection);
    }

    @Override
    public void del(Integer id) {
        iTbCollectionMapper.deleteById(id);
    }

    @Override
    public void set(TbCollection collection) {
        iTbCollectionMapper.updateById(collection);
    }

    @Override
    public TbCollection getById(Integer id) {
        return iTbCollectionMapper.selectById(id);
    }

    @Override
    public TbCollection getByAuthorAndPoetry(Integer userId, Integer poetryId) {
        QueryWrapper<TbCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", poetryId);
        queryWrapper.eq("poetryid", poetryId);

        return iTbCollectionMapper.selectOne(queryWrapper);
    }

    /**
     * TODO 根据用户ID获取收藏记录
     * @param userId
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TbCollection> getMapCollectionByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        Page<TbCollection> page = new Page<>(pageCurrent, pageSize);
        IPage<TbCollection> iPage = iTbCollectionMapper.selectMapCollectionByPage(page, userId);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;
    }
}
